
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Xml;
using Swaf.Call;
using Swaf.Config;


namespace Swaf.Container
{
	/// <summary>
	/// Summary description for CatalogEvents.
	/// </summary>
	public class CatalogEvents : IConfigPartner, IConfigHandler
	{
		protected static string[] s_validAttributes = new string[]{};
		
		private IDictionary m_eventHandlers = new Hashtable();
		
		public static readonly string CachingCatalogItemEvent = "CachingCatalogItem";
		
		public CatalogEvents()
		{
		}
		
		public CatalogEvents(IApplication app)
		{
		}

		/// <summary>
		/// Property containing all the event handlers for catalog events
		/// </summary>
		public IDictionary EventHandlers
		{
			get { return m_eventHandlers; }
		}

		#region IConfigPartner Members

		public void checkConfigStatus()
		{
		}

		public void registerForConfig(IConfigManager config)
		{
			config.registerHandler("...<CatalogEvents> <CachingCatalogItem>", this);
		}

		#endregion

		#region IConfigHandler Members

		public void executeBefore(IConfigElement configInfo)
		{
			IApplication app = Application.currentApp;

			IClassFactory callFactory = app.factories["CallTypes"];
			configInfo.checkIllegalAttributes(s_validAttributes);
			XmlElement ele = configInfo.Element;
			foreach(XmlNode child in ele.ChildNodes)
			{
				if (child.NodeType == XmlNodeType.Element)
				{
					IList list = m_eventHandlers[configInfo.Element.Name] as IList; 
					if (list == null)
					{
						// List not created for this type of event handler yet so created
						list = new ArrayList();
						m_eventHandlers[configInfo.Element.Name] = list;
					}
					
					// Add this handler to the list of event handlres
					list.Add((ICall)callFactory.createFromNickName(child.Name, "", child));
					
					break;
				}
			}
		}

		public void executeAfter(IConfigElement n)
		{
		}

		#endregion
		
	}
}
